/*
 * ConexionDao.java
 *
 * Created on 13 de agosto de 2007, 03:08 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ig.formato.dao;

import ig.formato.dao.abs.DAO;
import ig.formato.dto.GrupoDTO;
import ig.formato.util.IGFormatoException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author jsanchez
 */
public class GrupoDao extends DAO {

    /** Creates a new instance of ConexionDao */
    public GrupoDao() {
    }

    public Object buscar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            this.prep = this.connectionIgFormato.prepareStatement("SELECT ID,NOMBRE FROM GRUPO WHERE ID = ?");
            this.prep.setInt(1, id);
            this.resul = this.prep.executeQuery();
            if (this.resul.next()) {
                return getDatos(resul, new GrupoDTO());
            }
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            this.closeAll();
        }
        return null;
    }

    public Collection buscarTodos() throws IGFormatoException {
        Collection<Object> c = new ArrayList<Object>();
        try {
            /*connectionIgFormato = */            getConnection();
            this.prep = this.connectionIgFormato.prepareStatement("SELECT ID,NOMBRE FROM GRUPO");
            this.resul = this.prep.executeQuery();
            while (this.resul.next()) {
                boolean add = c.add(getDatos(resul, new GrupoDTO()));
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            this.closeAll();
        }
    }

    public Integer guardar(Object obj) throws IGFormatoException {
        GrupoDTO dto = (GrupoDTO) obj;
        try {
            Object _obj = this.existe(dto);
            if (!(_obj instanceof Boolean)) {
                return (Integer) _obj;
            }

            /*connectionIgFormato = */            getConnection();
            if (dto.getId() == null) {
                this.prep = this.connectionIgFormato.prepareStatement("INSERT INTO GRUPO(NOMBRE, ID) VALUES(?, ?)");
                dto.setId(this.getMaximoId("GRUPO"));
            } else {
                this.prep = this.connectionIgFormato.prepareStatement("UPDATE GRUPO SET NOMBRE=? WHERE ID=?");
            }
            setDatos(prep, dto, "NOMBRE,ID");
            this.prep.executeUpdate();
            return dto.getId();
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            this.closeAll();
        }
    }

    public void eliminar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            this.prep = this.connectionIgFormato.prepareStatement("DELETE FROM GRUPO WHERE ID = ?");
            this.prep.setInt(1, id);
            this.prep.executeUpdate();
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            this.closeAll();
        }
    }

    public Object existe(Object obj) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            this.prep = this.connectionIgFormato.prepareStatement("SELECT ID FROM GRUPO WHERE NOMBRE = ?");
            this.prep.setString(1, ((GrupoDTO) obj).getNombre().trim());
            this.resul = this.prep.executeQuery();
            if (resul.next()) {
                return resul.getInt("ID");
            }
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            this.closeAll();
        }
        return false;
    }


}


